package Algorithm;

public class removeKZeros_02 {
    public static String RemoveKzeros(String iniString,int k){
        char[] helper=iniString.toCharArray();
        StringBuilder stringBuilder=new StringBuilder();
        int count=0;
        int flag=0;
        for(int i=0;i<helper.length;i++){
            if(helper[i]=='0'){ //查找到0
                count++; //count进行++
            }else{//不是0就直接进行追加,同时将count置零
                if(count>=k){  //当count大于等于k，即有能力去除连续的k个0时，才进行去除
                    for(int j=0;j<count-k;j++){
                        stringBuilder.append('0');
                    }
                    //此时已经去除成功了连续的k个0
                    flag=1;
                    k=Integer.MAX_VALUE;  //防止再次进入循环
                }else{
                    for(int m=0;m<count;m++){
                        stringBuilder.append('0');
                    }
                }
                stringBuilder.append(helper[i]);
                count=0;
            }
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        String s1="1000001000000001";
        String s2="10001000001000010000";
        System.out.println(RemoveKzeros(s1,3));
        System.out.println(RemoveKzeros(s2,5));
    }
}
